<?php

namespace app\task\command;

use api\Xiaoju;
use app\api\service\OilStoreService;
use app\api\service\OilXiaojuService;
use app\model\BaseSupplier;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\Output;

/**
 * 定时向小桔 获取数据.
 * 相关接口文档地址 https://open-epower.xiaojukeji.com/oil.html#/oil-api-h5/unicomFile/73.
 */
class XiaojuApi extends Command
{
    public function __construct(Xiaoju $xiaoju)
    {
        parent::__construct();
        !defined('TP_START') && define('TP_START', microtime(true)); // 解决doLog 没有改变量问题
        $this->xiaoju = $xiaoju;
    }

    protected function configure()
    {
        $this->setName('xiaoju')
            ->addArgument('service', Argument::OPTIONAL, '参数')
            ->setDescription('小桔定时获取token');
    }

    /**
     * cli 入口 根据service不同做不同处理.
     */
    protected function execute(Input $input, Output $output)
    {
        switch ($input->getArgument('service')) {
            case 'queryToken':  // 刷 token
                $this->queryToken($input, $output);
            break;
            case 'queryStoreList':  // 查询加油站列表
                $this->queryStoreList($input, $output);
            break;
        }
    }

    /**
     * 刷新token.
     */
    protected function queryToken(Input $input, Output $output)
    {
        $res = invoke(OilXiaojuService::class)->queryToken();
        if ($res['code']) {
            $output->write('小桔token定期失败，token='.$res['msg']);
        } else {
            $output->write('queryToken over，token='.$res['data']['access_token']);
        }
    }

    /**
     * @api  获取加油站列表
     *
     * @param pageIndex  //页码 从1开始
     * @param pageSize //分页大小 单页上限100条
     */
    protected function queryStoreList(Input $input, Output $output)
    {
        $supplierInfo = BaseSupplier::where('code', OIL_SUPPLIER_ALIAS['xiaoju']['alias'])->find();
        $oilStoreService = new OilStoreService();
        $oilStoreService->getStoreFromApi($supplierInfo);
        $output->write('excute over');
    }
}
